﻿using System;
using System.ComponentModel.Composition;
using Caliburn.Micro;


namespace DailyFx.Studio.Menu
{
	using DailyFx.UI;
	using DailyFx.Application;
	using DailyFx.Book;

	[MenuExport]
	public class FolioPart : Menu
	{
		public FolioPart() 
			: base( "FOLIO", null)
		{
			// Empty .
		}
	}


	[MenuExport(MenuRoot = "FOLIO")]
	public class OpenPart : Menu
	{
		public OpenPart ()
			: base("OPEN", null)
		{
		}

		public override void RunCommand()
		{
			bool? result = Explorer.Show( null, FileExplorer.MODE_FILE_OPEN);

			// todo: fix it !!
			using (DailyFx.Book.ITable book = new DailyFx.Book.Repo.Book())
			{
				book.TryOpen( Explorer.FilePath );
				
				// resolves present bar's fields .
				// 
				Bar.FieldMask fieldMask = Bar.FieldMask.Unknown;

				int nDate	= book.Header.IndexOf("Date");
				int nClose	= book.Header.IndexOf("Close");
				int nOpen	= book.Header.IndexOf("Open");
				int nHigh	= book.Header.IndexOf("High");
				int nLow	= book.Header.IndexOf("Low");
				int nVol	= book.Header.IndexOf("Vol");

				// assert ( nDate != -1 && nClose != -1)
				fieldMask |= Bar.FieldMask.C;
				if ( nOpen != -1)
					fieldMask |= Bar.FieldMask.O;
				if ( nHigh != -1)
					fieldMask |= Bar.FieldMask.H;
				if ( nLow != -1)
					fieldMask = Bar.FieldMask.L;
				if ( nLow != -1 )
					fieldMask = Bar.FieldMask.VOL;


				// determines date time format .
				//
				Bar.Period period = Bar.Period.Daily;

				String p = null;
				bool ok = book.Metadata.TryGet("Period", out p);
				if ( ok )
				{
					System.Enum.TryParse<Bar.Period>(p, true, out period);
				}

				String format = null ;
				if (book.Header["Date"].Type == typeof(String))
				{
					if ((int)period < (int)Bar.Period.Daily) // intra-day type .
						format = "yyyy/MM/dd";
					else
						format = "yyyy/MM/dd HH:mm:ss";
				}


				// Build the bar series .
				//
				BarSeries series = new BarSeries( book.Records.Count );
				series.Period = (int)period;
				series.Mask = (int)fieldMask;

				foreach (var record in book.Records)
				{
					Bar bar = new Bar() ;
					
					// if ( nDate != -1)
					if ( format == null )
						bar.Tick = System.Convert.ToDateTime ( record[nDate] );
					else
						bar.Tick = System.DateTime.ParseExact( (String)record[nDate], format, null );

					// if ( nClose != -1 )
						bar.LastPx	= Convert.ToDecimal( record[nClose]);
					if ( nOpen != -1 )
						bar.Open	= Convert.ToDecimal( record[nOpen] );
					if ( nHigh != -1 )
						bar.High	= Convert.ToDecimal( record [nHigh] );
					if ( nLow != -1)
						bar.Low		= Convert.ToDecimal( record[nLow] );
					if ( nVol != -1 )
						bar.Vol		= Convert.ToDecimal( record[nVol] );

					series.Add ( bar);
				}

				String code;
				book.Metadata.TryGet("Code", out code);
				String symbol;
				book.Metadata.TryGet("Symbol", out symbol);


				IoC.Get<IStudio>().DailyFxSystem.enlist (code, series);

				CanvasImpl canvas = new CanvasImpl( symbol, series);
				IoC.Get<IStudio>().ChartManager.ApplyCanvas ( canvas );
			}
		}

		[Import]
		IExplorer Explorer { get; set; }
	}


	[MenuExport(MenuRoot = "FOLIO", MenuPrev="OPEN")]
	public class SavePart : Menu
	{
		public SavePart()
			: base("SAVE", null)
		{ 
		}
	}
}
